
using System;
using EPiServer.Common.Tags;

namespace EPiServer.Templates.RelatePlus.Templates.PageEntity.TagBox
{
    public partial class TagBox : EPiServer.UserControlBase
    {
        /// <summary>
        /// Gets or sets the TagCollection containing the tags displayed in this control. Set this value before data bind to specify the TagCollection to be displayed
        /// when this control is data bound. Once ReadClientData has been called, either implicitly via the OnClick event of the built-in save button 
        /// or explicitly from code when another button is used, this property contains the TagCollection input by the client.
        /// </summary>
        /// <value>The tags.</value>
        public TagCollection Tags { get; set; }

        /// <summary>
        /// Gets or sets the PageEntityType value displayed in this control. Set this value before data bind to specify the PageEntityType to be selected when 
        /// this control is data bound. Once ReadClientData has been called, either implicitly via the OnClick event of the built-in save button or explicitly 
        /// from code when another button is used, this property contains the data input by the client.
        /// </summary>
        /// <value>The PageEntityType selected by the client.</value>
        //public PageEntityTypes PageEntityType { get; set; }


        /// <summary>
        /// Occurs when the buit-in save button has been clicked and when Tags and PageEntityType properties have been updated with the data from the client.
        /// </summary>
        /// <remarks>
        /// Note that this event is triggered only when the built-in save button is used. If you specify that another button to be used, via the SubmitButtonClientID 
        /// property, you are responsible for calling ReadClientData when that button's click event triggers.
        /// </remarks>
        public event EventHandler ClientDataAvailable;
        private PageEntity _currentPageEntity;


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack)
            {
                //EntityTypesDropDown.DataSource = Enum.GetNames(typeof(PageEntityTypes));

                _currentPageEntity = PageEntityHandler.GetPageEntityByGUID(CurrentPage.PageGuid);

                TagCollection tagBoxTags = new TagCollection();

                if (_currentPageEntity == null)
                {
                    Tags = new TagCollection();
                }
                else
                {

                    foreach (EntityTag eTag in _currentPageEntity.EntityTags)
                    {
                        tagBoxTags.Add(eTag.Tag);
                    }

                    Tags = tagBoxTags;
                }

                InitInputHidden();
                //EntityTypesDropDown.SelectedIndex = (int)PageEntityType;

                DataBind();
            }
        }

        private void InitInputHidden()
        {
            string retValue = String.Empty;

            if (Tags == null)
            {
                InputHidden.Value = String.Empty;
                return;
            }

            foreach (Tag tag in Tags)
            {
                retValue += String.Format("{0}|{1},", tag.ID, tag.Name);
            }

            InputHidden.Value = retValue.TrimEnd(',');
        }

       

    }
}